#!/bin/sh

#   Forking processes
#
#   Copyright (C) 2009 Ultratronik Entwicklungs GmbH
#     - Author: Sören Wellhöfer <soeren.wellhoefer@gmx.net>.
#


# Defaults
FORK_PAUSE=5
NUM_FORKS=1
EXEC_PATH=""

usg() {
    echo "$0 [-num forks] [-pause interval] EXECUTABLE"
}

msg() {
    echo "$0: $1"
}

if test "$1" = "-help" || test "$1" = "-h"; then
    usg
    exit
fi 

while test ! -z "$1"; do
    case "$1" in

	"-pause")
	    shift
	    FORK_PAUSE=$1
	    ;;
	"-num")
	    shift
	    NUM_FORKS=$1
	    ;;

	*)
	    if test ! -z "$EXEC_PATH"; then
		msg "Invalid arguments supplied"
		exit
	    fi
	    EXEC_PATH=$1
	    ;;
    esac
    shift    
done

if test -z "$EXEC_PATH"; then
    usg
    exit
fi

while test $NUM_FORKS -gt 0; do
    NUM_FORKS=$(($NUM_FORKS-1))
    (exec $EXEC_PATH)&
    sleep $FORK_PAUSE
done

